package algorithms.leaning.class03;

import common.util.MyUtil;

/**
 * 数组实现栈和队列
 *
 * @author guichang
 * @date 2021/5/30
 */

@SuppressWarnings("all")
public class Code4_数组实现栈和队列 {

    public static void main(String[] args) {
//        ArrList list = new ArrList(2);
//        list.offer(1);
//        list.offer(2);
//        list.offer(3);
//        list.poll();
//        list.poll();
//        list.poll();
//        list.offer(4);
//        list.offer(5);
//        list.offer(6);
//        list.poll();
//        list.poll();
//        list.poll();

        ArrStack stack = new ArrStack(2);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.pop();
        stack.pop();
        stack.pop();
        stack.push(4);
        stack.push(5);
        stack.push(6);
        stack.pop();
        stack.pop();
        stack.pop();
    }

    private static class ArrList {
        int[] nums;
        int read, write, length;
        boolean full, empty;

        public ArrList(int size) {
            nums = new int[size];
            read = write = 0;
            length = size;
            full = false;
            empty = true;
        }

        public void offer(int i) {
            // 判断满了
            if (full) {
                MyUtil.printf("{} 满了", i);
                return;
            }
            nums[write] = i;
            MyUtil.printf("{} 成功", i);
            // 到最后了要从0开始
            write = write == length - 1 ? 0 : write + 1;
            // 没空了
            empty = false;
            // 追上了就满了
            if (write == read) {
                full = true;
            }
        }

        public int poll() {
            // 判断空了
            if (empty) {
                MyUtil.print("队列空的");
                return Integer.MAX_VALUE;
            }
            int v = nums[read];
            MyUtil.printf("{} 弹出", v);
            // 到最后了从0开始
            read = read == length - 1 ? 0 : read + 1;
            // 没满了
            full = false;
            // 追上了就空了
            if (read == write) {
                empty = true;
            }
            return v;
        }

        public int peek() {
            // 判断空了
            if (empty) {
                MyUtil.print("空的");
                return Integer.MAX_VALUE;
            }
            return nums[read];
        }
    }

    private static class ArrStack {
        int[] nums;
        int index, length;
        boolean full, empty;

        public ArrStack(int size) {
            nums = new int[size];
            index = -1; // 从-1开始，push前+1，pop后-1
            length = size;
            full = false;
            empty = true;
        }

        public void push(int i) {
            // 判断满了
            if (full) {
                MyUtil.printf("{} 满了", i);
                return;
            }
            nums[++index] = i;
            MyUtil.printf("{} 成功", i);
            empty = false;
            if (index == length - 1) {
                full = true;
            }
        }

        public int pop() {
            // 判断空了
            if (empty) {
                MyUtil.print("栈空的");
                return Integer.MAX_VALUE;
            }
            int v = nums[index--];
            MyUtil.printf("{} 弹出", v);
            full = false;
            if (index == -1) {
                empty = true;
            }
            return v;
        }

        public int peek() {
            // 判断空了
            if (empty) {
                MyUtil.print("栈空的");
                return Integer.MAX_VALUE;
            }
            return nums[index];
        }
    }

}